import { defineStore } from 'pinia';
import { Session } from '/@/utils/storage';
import userCenterApi from '/@/api-services/apis/system/userCenterApi';
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfosState => ({
		userInfos: {} as any,
	}),
	actions: {
		async setUserInfos() {
			// 存储用户信息到浏览器缓存
			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				const userInfos: any = await this.getApiUserInfo();
				this.userInfos = userInfos;
			}
		},
		// 模拟接口数据
		// https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
		async getApiUserInfo() {
			return new Promise((resolve) => {
				setTimeout(() => {
					userCenterApi.loginUserDetail()
					.then((res: any) => {
						if (res.data.data == null) return;
						var d = res.data.data;
						const userInfos = {
							userId : d.id,
							account: d.account,
							Name: d.name,
							avatar: d.avatar ? d.avatar : '/favicon.ico',
							address: d.address,
							signature: d.signature,
							orgId: d.orgId,
							orgName: d.orgName,
							posName: d.posName,
							authBtnList:d.buttons,
							time: new Date().getTime(),
						};
						resolve(userInfos);
					});
				}, 0);
			});
		},
	},
});
